<!-- 小米商城轮播图 -->
<template>
  <div class="banner">
    <div class="wrap">
      <!-- 轮播图主体 -->
      <div class="carousel" @mouseenter="handleMouseEnter" @mouseleave="handleMouseLeave">
        <ul class="pic">
          <li v-for="(item, index) in goods" :key="item.gid" v-show="currentIndex === index">
            <a href="#">
              <img :src="`../../../public/images/${item.img}`" :alt="'轮播图' + (index + 1)" />
            </a>
          </li>
        </ul>
        
        <!-- 轮播图指示器 -->
        <div class="indicators">
          <span 
            v-for="(item, index) in goods" 
            :key="index" 
            :class="{ active: currentIndex === index }"
            @click="currentIndex = index"
          ></span>
        </div>
        
        <!-- 左右箭头 -->
        <div class="arrow prev" @click="prevSlide">&#10094;</div>
        <div class="arrow next" @click="nextSlide">&#10095;</div>
      </div>
      <div class="slide">
        <ul>
          <li v-for="(items, category) in categorizedGoods" :key="category">
            <a href="#">{{ category }}</a><i class="iconfont">&#xe621;</i>
            <div class="slide-list">
              <ul>
                <li v-for="item in items" :key="item.gid">
                  <a href="#">
                    <router-link :to="`/goodsdetail?goodsid=${item.gid}`">
                    <img :src="`/images/${item.img}`" :alt="item.gname" />
                    <span>{{ item.gname }}</span>
                  </router-link>
                  </a>
                </li>
              </ul>
            </div>
          </li>
        
        </ul>
      </div>
    </div>
  </div>
  
</template>
<script setup>
import { ref, onMounted, onBeforeUnmount,computed } from 'vue'
import axios from '../../api/axios';

const loading = ref(true);
const goods = ref([]); // 存储轮播图数据
const currentIndex = ref(0);
let timer = null;

// 侧边栏数据
const allGoods = ref([]);
axios.post('/api/goods/lunbo')
  .then((res) => {
    //console.log('Goods response:', res.data); // 添加调试信息
    goods.value = res.data.goods; // 确保赋值给goods而不是goodsList
    loading.value = false;
  })
  .catch(() => {
    loading.value = false;
  });

  // 获取所有商品数据（用于侧边栏）
axios.post('/api/goods/all')
  .then((res) => {
    allGoods.value = res.data.goods;
  });

// 分类商品数据
const categorizedGoods = computed(() => {
  return allGoods.value.reduce((acc, item) => {
    if (!acc[item.cgname]) {
      acc[item.cgname] = [];
    }
    acc[item.cgname].push(item);
    return acc;
  }, {});
});
// 下一张
const nextSlide = () => {
  if (goods.value.length > 0) {
    currentIndex.value = (currentIndex.value + 1) % goods.value.length;
  }
}

// 上一张
const prevSlide = () => {
  if (goods.value.length > 0) {
    currentIndex.value = (currentIndex.value - 1 + goods.value.length) % goods.value.length;
  }
}

// 自动轮播
const startAutoPlay = () => {
  timer = setInterval(nextSlide, 3000);
}

// 停止自动轮播
const stopAutoPlay = () => {
  if (timer) clearInterval(timer);
}

// 鼠标悬停时暂停轮播
const handleMouseEnter = () => {
  stopAutoPlay();
}

// 鼠标离开时恢复轮播
const handleMouseLeave = () => {
  startAutoPlay();
}

onMounted(() => {
  startAutoPlay();
});

onBeforeUnmount(() => {
  stopAutoPlay();
});
</script>

<style scoped>
.banner {
  position: relative;
  width: 100%;
  height: 460px;
  overflow: hidden;
  background-color: #f5f5f5;
}

.wrap {
  position: relative;
  width: 1226px;
  height: 100%;
  margin: 0 auto;
}

.carousel {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.pic {
  width: 100%;
  height: 100%;
  position: relative;
}

.pic li {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  transition: opacity 0.5s ease;
}

.pic img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.indicators {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 8px;
}

.indicators span {
  display: block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background-color: rgba(255, 255, 255, 0.5);
  cursor: pointer;
  transition: background-color 0.3s;
}

.indicators span.active {
  background-color: #fff;
}

.arrow {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 40px;
  height: 80px;
  background-color: rgba(0, 0, 0, 0.2);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  cursor: pointer;
  user-select: none;
  transition: background-color 0.3s;
}

.arrow:hover {
  background-color: rgba(0, 0, 0, 0.5);
}

.prev {
  left: 240px; /* 向右移动240像素 */
  border-top-right-radius: 4px;
  border-bottom-right-radius: 4px;
}

.next {
  right: 0px; 
  border-top-left-radius: 4px;
  border-bottom-left-radius: 4px;
}


</style>
